CLASS zcl_moon_if_log_api DEFINITION
  PUBLIC
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES if_http_service_extension .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS ZCL_MOON_IF_LOG_API IMPLEMENTATION.


  METHOD if_http_service_extension~handle_request.

    DATA:BEGIN OF ls_data,
           ifname       TYPE zcm_p_0003_mes-ifname,
           iftime       TYPE zcm_p_0003_mes-iftime,
           ifseq        TYPE zcm_p_0003_mes-ifseq,
           direction1   TYPE zcm_p_0003_mes-direction1,
           direction2   TYPE zcm_p_0003_mes-direction2,
           status1      TYPE zcm_p_0003_mes-status1,
           status2      TYPE zcm_p_0003_mes-status2,
           message1     TYPE zcm_p_0003_mes-message1,
           message2     TYPE zcm_p_0003_mes-message2,
           cpimessageid TYPE zcm_p_0003_mes-cpimessageid,
           keyfield     TYPE zcm_p_0003_mes-keyfield,
           keyspec      TYPE zcm_p_0003_mes-keyspec,
         END OF ls_data.
    DATA:lt_data LIKE TABLE OF ls_data.
    DATA:lv_req_json TYPE string,
         lv_res_json TYPE string.
    DATA:lt_dbtable TYPE TABLE OF zcm_t_0003_mes,
         ls_dbtable LIKE LINE OF lt_dbtable.
    DATA:lv_error.

    DATA:lt_eml_tb TYPE TABLE FOR CREATE zcm_p_0003_mes,
         ls_eml_tb LIKE LINE OF lt_eml_tb.


    lv_req_json = request->get_text( ).
    DATA(if_type) = request->get_form_field('IF_TYPE').

    CASE if_type.
      WHEN 'CREATE'.
        /ui2/cl_json=>deserialize(
           EXPORTING json = lv_req_json
                     pretty_name = /ui2/cl_json=>pretty_mode-low_case
           CHANGING data = lt_data
           ).

        LOOP AT lt_data INTO ls_data.
          MOVE-CORRESPONDING ls_data TO ls_dbtable.
          APPEND ls_dbtable TO lt_dbtable.

          MOVE-CORRESPONDING ls_data TO ls_eml_tb.
          ls_eml_tb-%cid = 'CID' && ls_data-ifseq.
          ls_eml_tb-%control = VALUE #( ifname = if_abap_behv=>mk-on
                                       iftime = if_abap_behv=>mk-on
                                       ifseq = if_abap_behv=>mk-on
                                       direction1 = if_abap_behv=>mk-on
                                       direction2 = if_abap_behv=>mk-on
                                       status1 = if_abap_behv=>mk-on
                                       status2 = if_abap_behv=>mk-on
                                       message1 = if_abap_behv=>mk-on
                                       message2 = if_abap_behv=>mk-on
                                       cpimessageid = if_abap_behv=>mk-on
                                       keyfield = if_abap_behv=>mk-on
                                       keyspec = if_abap_behv=>mk-on
                                      ).
          APPEND ls_eml_tb TO lt_eml_tb.
        ENDLOOP.

        MODIFY ENTITIES OF zcm_p_0003_mes
        ENTITY zcm_p_0003_mes
        CREATE FROM lt_eml_tb
           MAPPED   DATA(lt_mapped_modify)
           FAILED   DATA(lt_failed_modify)
           REPORTED DATA(lt_reported_modify).


*        Change
        IF sy-subrc <> 0.
          lv_error = abap_true.
        ENDIF.
      WHEN 'UPDATE'.
        /ui2/cl_json=>deserialize(
           EXPORTING json = lv_req_json
                     pretty_name = /ui2/cl_json=>pretty_mode-low_case
           CHANGING data = ls_data
           ).

        UPDATE zcm_t_0003_mes
        SET direction2 = @ls_data-direction2,
            status2 = @ls_data-status2,
            message2 = @ls_data-message2
        WHERE cpimessageid = @ls_data-cpimessageid .
        IF sy-subrc <> 0.
          lv_error = abap_true.
        ENDIF.
    ENDCASE.


    IF lv_error IS INITIAL.
      COMMIT WORK.
      lv_res_json = 'S'.
    ELSE.
      ROLLBACK WORK.
      lv_res_json = 'E'.
    ENDIF.



    response->set_text( lv_res_json ).
  ENDMETHOD.
ENDCLASS.
